package com.masterclient.app.modules.order.contract;

import android.widget.Toast;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselibrary.bean.order.ConfirmOrderBean;
import com.eoner.baselibrary.bean.order.OrderSendDeliveryBean;
import com.eoner.baselibrary.bean.order.SaleOrderDetailData;
import com.eoner.baselibrary.utils.Utils;
import com.masterclient.app.httpservice.RetrofitUtil2;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
* Created by TMVPHelper on 2017/07/28
*/
public class SaleOrderDetailPresenter extends SaleOrderDetailContract.Presenter{

    @Override
    public void confirm_order(String buy_product, String buy_type, String charge_account,String sale_type,String relation_order_id,String is_custody) {
        JSONObject jsonObject = new JSONObject();
        try {
            if (buy_product!=null) {
                JSONObject buyProduct = new JSONObject(buy_product);
                jsonObject.put("buy_product",buyProduct);
            }
            jsonObject.put("buy_type",Integer.parseInt(buy_type));
            jsonObject.put("sale_type",sale_type);
            jsonObject.put("charge_account",charge_account);
            jsonObject.put("relation_order_id",relation_order_id);
            jsonObject.put("is_custody",is_custody);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<ConfirmOrderBean> observable = RetrofitUtil2.getInstance().confirmOrder(body);
        Disposable disposable = toSubscribe(observable, new Consumer<ConfirmOrderBean>() {
            @Override
            public void accept(@NonNull ConfirmOrderBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.confirm_order_success(bean.getData(),sale_type, buy_type);
                }else{
                    mView.confirm_order_failed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                mView.confirm_order_failed(o.getMessage());
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void getOrderDetail(String order_no) {
        Disposable disposable = toSubscribe(RetrofitUtil2.getInstance().saleOrderDetail(order_no), new Consumer<CommonBaseResponse<SaleOrderDetailData>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<SaleOrderDetailData> bean) throws Exception {
                if(bean.getCode()==0){
                    mView.showOrderDetail(bean.getData());
                }else {
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }


    @Override // pick_up 提货， delivery_good 拨货 check_send自己发货
    public void checkGoods(String order_id, String type) {
        mRxManager.add(toSubscribe("pick_up".equals(type)?RetrofitUtil2.getInstance().pickUpGoods(order_id):"delivery_good".equals(type)?
                        RetrofitUtil2.getInstance().checkDeliveryGoods(order_id):RetrofitUtil2.getInstance().checkSend(order_id),
                new Consumer<CommonBaseResponse<Map<String,String>>>() {
                    @Override
                    public void accept(@NonNull CommonBaseResponse<Map<String,String>> bean) throws Exception {
                        if(bean.getCode() == 0){
                            Map<String, String> map = bean.getData();
                            map.put("relation_order_id",order_id);
                            map.put("type",type);
                            mView.checkGoodsSuccess(map);
                        }else {
                            ToastUtil.showToast(bean.getMsg());
                        }
                    }
                }, new ThrowableConsumer(){
                    @Override
                    public void accept(Throwable o) throws Exception {
                        super.accept(o);
                        ToastUtil.showToast("网络异常，请重试");
                    }
                }));
    }

    @Override
    public void sendDelivery(String order_id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("order_id",order_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().toSendDelivery(body), new Consumer<CommonBaseResponse<OrderSendDeliveryBean>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<OrderSendDeliveryBean> bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.sendDeliverySuccess(bean.getData());
                }else {
                    ToastUtil.showToast(bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                ToastUtil.showToast("网络异常，请重试");
            }
        }));
    }

    @Override
    public void upReserve(String order_id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id",order_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().upReserve(body), new Consumer<CommonBaseResponse>() {
            @Override
            public void accept(@NonNull CommonBaseResponse bean) throws Exception {
                if(bean.getCode() == 0){
                    ToastUtil.showToast("让上级代发成功");
                    mView.operateSuccess(0);
                }else {
                    ToastUtil.showToast(bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                ToastUtil.showToast("网络异常，请重试");
            }
        }));
    }
}